import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:oktoast/oktoast.dart';
import 'package:toatool/src/data/provider/login_provider.dart';
import 'package:toatool/src/data/remote/app_exceptions.dart';
import 'package:toatool/src/widgets/view_state.dart';

class ViewStateController extends GetxController {
  var viewState = ViewState.busy;
  late ViewStateError viewStateError;
  late bool isRouteGuard;

  bool get isBusy => viewState == ViewState.busy;

  bool get isIdle => viewState == ViewState.idle;

  bool get isEmpty => viewState == ViewState.empty;

  bool get isError => viewState == ViewState.error;

  /// set
  void setIdle() {
    viewState = ViewState.idle;
  }

  void setBusy() {
    viewState = ViewState.busy;
  }

  void setEmpty() {
    viewState = ViewState.empty;
  }

  /// [e]分类Error和Exception两种
  void setError(dynamic e, dynamic stackTrace, {required String message}) {
    late ViewStateErrorType errorType;

    /// 见https://github.com/flutterchina/dio/blob/master/README-ZH.md#dioerrortype
    if (e is HttpException) {
      errorType = ViewStateErrorType.httpException;
    } else if (e is FetchDataException) {
      errorType = ViewStateErrorType.fetchDataException;
    } else if (e is UnknowException) {
      errorType = ViewStateErrorType.unknowException;
    } else if (e is CancelException) {
      errorType = ViewStateErrorType.cancelException;
    } else if (e is NetworkException) {
      errorType = ViewStateErrorType.networkException;
    } else if (e is UnauthorizedException) {
      errorType = ViewStateErrorType.unAuthorizedException;
    } else {
      errorType = ViewStateErrorType.unknowException;
    }
    viewState = ViewState.error;
    viewStateError = ViewStateError(
      errorType,
      message: message,
      errorMessage: e.toString(),
    );
    printErrorStack(e, stackTrace);
    onError(viewStateError);
    showErrorMessage(e);
  }

  void onError(ViewStateError viewStateError) async {
    // 未认证
    if (isRouteGuard == true) {
      var loginProvider = Get.find<LoginProvider>();
      var isExistToken = loginProvider.isLogin();
      if ((isError == true && viewStateError.isUnAuthorizedException == true) ||
          !isExistToken) {
        // await Get.offNamed<void>(Routes.loginView, arguments: Get.arguments);
      }
    }
  }

  // 显示错误消息
  void showErrorMessage(dynamic e) {
    late String message;
    if (e is HttpException) {
      message = 'exception_of_http'.tr;
    } else if (e is FetchDataException) {
      message = 'exception_of_other'.tr;
    } else if (e is UnknowException) {
      message = 'exception_of_unknown'.tr;
    } else if (e is CancelException) {
      message = 'exception_of_request_cancel'.tr;
    } else if (e is NetworkException) {
      message = 'exception_of_network'.tr;
    } else if (e is UnauthorizedException) {
      message = 'exception_of_unauthorized'.tr;
    } else {
      message = 'exception_of_data'.tr;
    }
    Future.microtask(() {
      showToast(message, position: ToastPosition.top);
    });
  }

  @override
  String toString() {
    return 'BaseModel{_viewState: $viewState, _viewStateError: $viewStateError}';
  }
}

/// [e]为错误类型 :可能为 Error , Exception ,String
/// [s]为堆栈信息
void printErrorStack(dynamic e, dynamic s) {
  debugPrint('''
  <-----↓↓↓↓↓↓↓↓↓↓-----error-----↓↓↓↓↓↓↓↓↓↓----->
  $e
  <-----↑↑↑↑↑↑↑↑↑↑-----error-----↑↑↑↑↑↑↑↑↑↑----->''');
  if (s != null) {
    debugPrint('''
  <-----↓↓↓↓↓↓↓↓↓↓-----trace-----↓↓↓↓↓↓↓↓↓↓----->
  $s
  <-----↑↑↑↑↑↑↑↑↑↑-----trace-----↑↑↑↑↑↑↑↑↑↑----->
      ''');
  }
}
